package com.ruoyi.zyt.controller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zyt.domain.Aspiration;
import com.ruoyi.zyt.service.IAspirationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 志愿填报Controller
 *
 * @author zyt
 * @date 2023-11-05
 */
@RestController
@RequestMapping("/zyt/aspiration")
public class AspirationController extends BaseController {
    @Autowired
    private IAspirationService aspirationService;

    /**
     * 查询志愿填报列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Aspiration aspiration) {
        startPage();
        List<Aspiration> list = aspirationService.selectAspirationList(aspiration);
        return getDataTable(list);
    }

    /**
     * 导出志愿填报列表
     */
    @Log(title = "志愿填报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Aspiration aspiration) {
        List<Aspiration> list = aspirationService.selectAspirationList(aspiration);
        ExcelUtil<Aspiration> util = new ExcelUtil<Aspiration>(Aspiration.class);
        util.exportExcel(response, list, "志愿填报数据");
    }

    /**
     * 获取志愿填报详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(aspirationService.selectAspirationById(id));
    }

    /**
     * 新增志愿填报
     */
    @Log(title = "志愿填报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Aspiration aspiration) {
        Aspiration aspiration1 = new Aspiration();
        aspiration1.setUserId(aspiration.getUserId());
        List<Aspiration> aspirations = aspirationService.selectAspirationList(aspiration1);
        if (aspirations.size() == 0) {
            aspiration.setSort(1L);
        } else {
            aspirations.sort((a, b) -> {
                long l = a.getSort() - b.getSort();
                return (int) l;
            });
            Long sort = aspirations.get(0).getSort();
            aspiration.setSort(sort + 1);
        }

        return toAjax(aspirationService.insertAspiration(aspiration));
    }

    /**
     * 修改志愿填报
     */
    @Log(title = "志愿填报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Aspiration aspiration) {
        return toAjax(aspirationService.updateAspiration(aspiration));
    }

    /**
     * 删除志愿填报
     */
    @Log(title = "志愿填报", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(aspirationService.deleteAspirationByIds(ids));
    }

    @GetMapping("/evaluate")
    public AjaxResult evaluate(String userId) {

        return success(aspirationService.evaluate(userId));

    }
}
